// pages/phone/phone.js
const app = getApp()
const util = require('../../utils/util.js')

Page({
  /**
   * 页面的初始数据
   */
  data: {
    phone: null,
    newphone: null
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      phone: app.globalData.phone
    })
  },

  formSubmit: util.throttle(function(e) {
    let phone = this.data.phone
    let username = app.globalData.username
    let newphone = e.detail.value.newphone
    if (newphone == '' || newphone == null || !(/^1[34578]\d{9}$/.test(newphone))) {
      wx.showModal({
        content: "新号码为空 或 新号码格式不对！",
        showCancel: false,
        success: res => {
          if (res.confirm) {
            //输入的号码设为空
          }
        }
      })
    } else if (phone == newphone) {
      wx.showModal({
        content: "新号码与旧号码相同，请重新输入新密码",
        showCancel: false,
        // success: res => {
        //   if (res.confirm) {
        //     wx.reLaunch({
        //       url: '/pages/phone/phone'
        //     })
        //   }
        // }
      })
    } else
      wx.request({
        url: app.userUrl + '/WeChat/AccountManagement/updateUser',
        method: 'POST',
        data: {
          username: app.globalData.username,
          phone: newphone
        },
        success: (res) => {
          if (res.data == "success") {
            wx.showModal({
              content: '号码修改成功',
              showCancel: false,
              success: res => {
                app.globalData.phone = newphone
                if (res.confirm) {
                  wx.reLaunch({
                    url: '/pages/mine/mine'
                  })
                }
              }
            })
          } else {
            wx.showModal({
              content: '号码修改失败',
              showCancel: false,
              success: res => {
                if (res.confirm) {
                  this.setData({
                    newphone: ''
                  })
                }
              }
            })
          }
        },
        fail: res => {
          wx.showToast({
            title: '网络异常！',
            image: '/images/warning.png'
          })
        }
      })
  }),

})